#include "payload.h"
#include <QByteArray>
#include <QDataStream>
#include <QRandomGenerator>
#include <QDebug>

namespace  {

inline uint8_t bcd(int max)
{
    int h = QRandomGenerator::global()->bounded(max);
    int r = (h / 10 * (0x10 - 10) + h);
    return r;
}

inline uint8_t hour() { return bcd(24);}
inline uint8_t minute() { return bcd(60); }
}
QByteArray getPayload()
{
    QByteArray payload;
    {
        double longtitude = (QRandomGenerator::global()->bounded(360.0) - 180.0);
        // 似乎不支持同时读写
        //            https://stackoverflow.com/questions/38163181/read-write-on-byte-array-by-qdatastream
        QDataStream stream(&payload, QIODevice::ReadWrite);
        stream << (uint8_t)(QRandomGenerator::global()->bounded(100))   //age
               << (uint8_t)0
               << (uint8_t)minute()
               << (int16_t)(QRandomGenerator::global()->bounded(180))   //height
               << (uint16_t)(QRandomGenerator::global()->bounded(79.8)*100)
               << (uint32_t)0x30b3   // bedroom:1 kitchen:0b1011 rgb:0b0011
               << (int32_t)(longtitude / (8.38190317e-8)) ;  // longitude
        qDebug() << ">" << "longtitude=" << longtitude;
        Q_ASSERT(QDataStream::Ok == stream.status());
    }
    return payload;
}

